/*
 * 文件名：StsPackCollectServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月23日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.sts.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.job.bpa.service.IncaeSureService;
import top.infopub.job.bpa.service.ProContractService;
import top.infopub.job.fia.service.PayBillDetailService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.sts.service.PackCollectService;
import top.infopub.job.sts.service.StsPackCollectService;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.sts.PackCollect;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;


@Service
public class StsPackCollectServiceImpl implements StsPackCollectService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private IncaeSureService incaeSureService;

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private PackCollectService packCollectService;

    @Autowired
    private PayBillDetailService payBillDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsPackCollectJob() {
        //查询所有项目合同名称
        Map<String, Object> proContractWhere = Maps.newHashMap();
        proContractWhere.put("processStatus", "9");
        List<ProContract> proContracts = proContractService.selectByMapWhere(proContractWhere);
        for (ProContract proContract : proContracts) {
            PackCollect packCollect = new PackCollect();
            //往表里存放项目信息
            packCollect.setProCode(proContract.getProCode());
            packCollect.setProName(proContract.getProName());
            //往表里存放公司信息
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setProjectCode(proContract.getProCode());
            orgInfo.setState(1);
            List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
            if (orgInfos.size() > 0) {
                // 注意：不归集南充项目
                if (StringUtils.endsWithIgnoreCase(orgInfos.get(0).getId(),
                    BizParaUtil.get("land_org_id"))) {
                    continue;
                }
                packCollect.setOrgProName(orgInfos.get(0).getCompName());
                packCollect.setHrOrgProId(orgInfos.get(0).getId());
                OrgInfo parentOrgInfo = new OrgInfo();
                parentOrgInfo.setId(orgInfos.get(0).getParentId());
                parentOrgInfo.setState(1);
                List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
                if (parentOInfos.size() > 0) {
                    packCollect.setHrOrgAreaName(parentOInfos.get(0).getCompName());
                    packCollect.setHrOrgAreaId(parentOInfos.get(0).getId());
                }
            }
            //存放时间信息
            packCollect.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            packCollect.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            packCollect.setStsMonth(ParserUtil.prevMonth());//上个月
            //当月产值(下浮后)对内(元)
            Map<String, Object> innerSureWhere = Maps.newHashMap();
            innerSureWhere.put("sureMonth", ParserUtil.currentMonth());
            innerSureWhere.put("proCode", packCollect.getProCode());
            BigDecimal inSureAfter = incaeSureService.selectProQaOfInnerSureSum(innerSureWhere);
            packCollect.setAddupInSure(inSureAfter);
            //开累应付金额
            Map<String, Object> applyAmountWhere = Maps.newHashMap();
            applyAmountWhere.put("payDate", ParserUtil.currentMonth());
            applyAmountWhere.put("proCode", packCollect.getProCode());
            BigDecimal addupWillPay = payBillDetailService.selectPayBillApplyAmountSum(applyAmountWhere);
            packCollect.setAddupWillPay(addupWillPay);
            //开累实付金额
            Map<String, Object> actPayAmountWhere = Maps.newHashMap();
            actPayAmountWhere.put("payDate", ParserUtil.currentMonth());
            actPayAmountWhere.put("proCode", packCollect.getProCode());
            BigDecimal addupActPay = payBillDetailService.selectPayBillActPayAmountSum(actPayAmountWhere);
            packCollect.setAddupActPay(addupActPay);
            //开累应付未付金额(应付金额-实付金额)
            BigDecimal addupNoPay = addupWillPay.subtract(addupActPay);
            packCollect.setAddupNoPay(addupNoPay);
            //数据状态(默认为有效)
            packCollect.setDataFlag("1");
            //添加人 添加时间 最后修改人 最后修改时间
            packCollect.setAddBy("system");
            packCollect.setAddTime(new Date());
            packCollect.setUptBy("system");
            packCollect.setUptTime(new Date());
            //修改同一个项目下除最新的项目外，其他历史记录数据状态为0(无效的)
            Map<String, Object> map = Maps.newHashMap();
            map.put("dataFlag", "0");
            map.put("proCode", packCollect.getProCode());
            packCollectService.updateByProCode(map);
            //添加数据
            packCollectService.insert(packCollect);
        }
    }
}